package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.VerOrderItem;
import com.ruoyi.order.domain.dto.ScrollPageRequest;
import com.ruoyi.order.domain.dto.ScrollPageResponse;
import com.ruoyi.order.domain.dto.VerOrderItemDto;
import com.ruoyi.order.mapper.VerOrderItemMapper;
import com.ruoyi.order.service.IVerOrderItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单项Controller
 *
 * @author ruoyi
 * @date 2025-08-21
 */
@RestController
@Tag(name = "订单项接口", description = "提供订单项操作")
@RequestMapping("/order/orderItem")
public class VerOrderItemController extends BaseController {
    @Autowired
    private IVerOrderItemService verOrderItemService;
    @Autowired
    private VerOrderItemMapper verOrderItemMapper;

    /**
     * 查询订单项列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:list')")
    @Operation(summary = "查询订单项列表", description = "传入订单项查询对象，返回订单项列表")
    @PostMapping("/list")
    public TableDataInfo list(VerOrderItem verOrderItem) {
        startPage();
        List<VerOrderItem> list = verOrderItemService.selectVerOrderItemList(verOrderItem);
        return getDataTable(list);
    }

    /**
     * 滚动分页查询订单项列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:list')")
    @Operation(summary = "滚动分页查询订单项列表", description = "传入滚动分页请求，返回订单项列表")
    @GetMapping("/scroll")
    public AjaxResult scrollList(ScrollPageRequest request) {
        ScrollPageResponse<VerOrderItem> response = verOrderItemService.selectVerOrderItemListByScroll(request);
        return success(response);
    }

    /**
     * 导出订单项列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:export')")
    @Operation(summary = "导出订单项列表", description = "传入订单项查询对象，返回订单项列表")
    @Log(title = "订单项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VerOrderItem verOrderItem) {
        List<VerOrderItem> list = verOrderItemService.selectVerOrderItemList(verOrderItem);
        ExcelUtil<VerOrderItem> util = new ExcelUtil<VerOrderItem>(VerOrderItem.class);
        util.exportExcel(response, list, "订单项数据");
    }

    /**
     * 获取订单项详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:query')")
    @Operation(summary = "获取订单项详细信息", description = "传入订单项 ID，返回订单项详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(verOrderItemService.selectVerOrderItemById(id));
    }

    /**
     * 新增订单项
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:add')")
    @Operation(summary = "新增订单项", description = "传入订单项对象，返回新增结果")
    @Log(title = "订单项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VerOrderItem verOrderItem) {
        return toAjax(verOrderItemService.insertVerOrderItem(verOrderItem));
    }

    /**
     * 修改订单项
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:edit')")
    @Log(title = "订单项", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改订单项", description = "传入订单项对象，返回修改结果")
    @PutMapping
    public AjaxResult edit(@RequestBody VerOrderItem verOrderItem) {
        return toAjax(verOrderItemService.updateVerOrderItem(verOrderItem));
    }

    /**
     * 删除订单项
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:remove')")
    @Operation(summary = "删除订单项", description = "传入订单项 ID，返回删除结果")
    @Log(title = "订单项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(verOrderItemService.deleteVerOrderItemByIds(ids));
    }

    /**
     * 根据订单ID获取订单项详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:query')")
    @Operation(summary = "根据订单ID获取订单项详细信息", description = "传入订单ID，返回订单项详细信息")
    @PostMapping("/order/{orderId}")
    public AjaxResult getInfoByOrderId(@PathVariable("orderId") Long orderId) {
        VerOrderItemDto dto = verOrderItemService.selectVerOrderItemByOrderId(orderId);
        return success(dto);
    }
    /**
     * 修改订单项状态
     */
    @PreAuthorize("@ss.hasPermi('order:orderItem:edit')")
    @Log(title = "订单项", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改订单项状态", description = "传入订单项对象，返回修改结果")
    @PutMapping("/status")
    public AjaxResult editStatus(@RequestParam Long orderItemId, @RequestParam Integer isReviewed) {
        return toAjax(verOrderItemMapper.updateOrderItemStatus(orderItemId, isReviewed));
    }
}
